package com.lanou.musicproject.net;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.lanou.musicproject.bean.NewSong;
import com.lanou.musicproject.bean.RecommHotSong;
import com.lanou.musicproject.bean.Rotate;
import com.lanou.musicproject.bean.SongListRecomm;

import org.json.JSONObject;

/**
 * Created by 王世美 on 2015/10/23.
 * 网络拉取得工具类
 */
public class HttpHelper implements NetDataWhat {
    private RequestQueue requestQueue;//请求队列
    private NetDataCallBack netDataCallBack;//返回数据的接口

    public HttpHelper(NetDataCallBack netDataCallBack) {
        this.netDataCallBack = netDataCallBack;
        requestQueue = MyQueue.getInstance().getVolleyQueue();
    }

    /**
     * 推荐-轮播 数据接口
     */
    public void getRotatePage() {
        String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=" +
                "5.6.5.0&method=baidu.ting.plaza.getFocusPic&format=json&num=6";
        getNetData(path,WHAT_ROTATE_PAGE);
    }

    /**
     * 推荐-歌单推荐 数据接口
     */
    public void getSongListPage(){
        String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&" +
                "method=baidu.ting.diy.getHotGeDanAndOfficial&format=json&num=6";
        getNetData(path,WHAT_SONGLIST);
    }

    /*
    * 推荐-热门歌曲推荐 数据接口
    */
    public void getHotSongPage() {
        String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&" +
                "method=baidu.ting.song.getEditorRecommend&format=json&num=6";
        getNetData(path,WHAT_HOT_SONG);
    }

    /*
    * 推荐-最新歌曲 数据接口
    */
    public void getNewSongPage() {
        String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0" +
                "&method=baidu.ting.plaza.getRecommendAlbum&format=json&offset=0&limit=2";
        getNetData(path,WHAT_NEW_SONG);
    }

    //网络数据拉取
    private void getNetData(String path, final int what) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(path,
                null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                /**成功，调用接口的成功方法
                 * @pargm response 解析之后的数据实体类
                 */
                netDataCallBack.netSuccess(what,resolution(what,response));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //失败，调用接口的失败方法
                netDataCallBack.netError(what);
            }
        });
        requestQueue.add(jsonObjectRequest);//加入到请求队列
    }

    //解析 方法，将Json数据解析成实体类
    private Object resolution(int what,JSONObject response){
        Object object = null;
        switch (what){
            case WHAT_ROTATE_PAGE:
                object = JSON.parseObject(response.toString(), Rotate.class);
                break;
            case WHAT_SONGLIST:
                object = JSON.parseObject(response.toString(), SongListRecomm.class);
                break;
            case WHAT_HOT_SONG:
                object = JSON.parseObject(response.toString(), RecommHotSong.class);
                break;
            case WHAT_NEW_SONG:
                object = JSON.parseObject(response.toString(), NewSong.class);
                break;
        }
            return object;
    }
}
